package com.ih.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ih.mapper.HistoryMapper;
import com.ih.pojo.History;
import com.ih.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private HistoryMapper historyMapper;

    @Override
    public List<History> queryHistory() {
        return historyMapper.selectList(null);
    }

    @Override
    public PageInfo<History> listHistory(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        List<History> list = historyMapper.listHistory(map);
        return new PageInfo<>(list);
    }

    /**
     * 添加历史信息
     *
     * @param history
     * @return
     */
    @Override
    public int saveHistory(History history) {
        return historyMapper.saveHistory(history);
    }

    /**
     * 根据id查找
     *
     * @param hid
     * @return
     */
    @Override
    public History getHistoryById(Integer hid) {

        return historyMapper.getHistoryById(hid);
    }

    /**
     * 修改方法
     * @param history
     * @return
     */
    @Override
    public int updateHistory(History history) {
        return historyMapper.updateHistory(history);
    }

    /**
     * 批量删除
     *
     * @param idsList
     * @return
     */
    @Override
    public int batchDeleteHistory(String idsList) {
        String[] split = StrUtil.split(idsList, ",");
        List<Integer> ids=new ArrayList<>();
        for (String id : split) {
            if (StrUtil.isNotEmpty(id)){
                ids.add(Integer.parseInt(id));
            }
        }
        return historyMapper.batchDeleteHistory(ids);
    }
}
